<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="仓库" prop="warehouseDeptId">
                <el-input v-model="queryParams.warehouseDeptId" placeholder="请输入仓库" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="入库单号" prop="warehousingRequisitionOrder">
                <el-input v-model="queryParams.warehousingRequisitionOrder" placeholder="请输入入库单号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="业务员" prop="warehousingRequisitionPerson">
                <el-input v-model="queryParams.warehousingRequisitionPerson" placeholder="请输入业务员" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="供应商" prop="supplierId">
                <el-input v-model="queryParams.supplierId" placeholder="请输入供应商" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['warehousingRequisition:warehousingRequisition:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['warehousingRequisition:warehousingRequisition:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['warehousingRequisition:warehousingRequisition:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['warehousingRequisition:warehousingRequisition:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="warehousingRequisitionList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" prop="warehousingRequisitionId" />
            <el-table-column label="入库单号" align="center" prop="warehousingRequisitionOrder" >
            <template slot-scope="scope">
                    <router-link :to="'/warehousing-table/warehousing/' + scope.row.warehousingRequisitionId"
                        class="link-type">
                        <span>{{ scope.row.warehousingRequisitionOrder }}</span>
                    </router-link>
                </template>
            </el-table-column>
            <el-table-column label="业务员" align="center" prop="warehousingRequisitionPerson" />
            <el-table-column label="供应商" align="center" prop="supplier.supplierName" width="120px" />
            <el-table-column label="采购类型" align="center" prop="requisitionType">
                <template slot-scope="scope">
                    <span v-if="scope.row.requisitionType == 0">
                        <el-tag type="success">普通采购</el-tag>
                    </span>
                    <span v-if="scope.row.requisitionType == 1">
                        <el-tag type="danger">紧急采购</el-tag>
                    </span>

                </template>
            </el-table-column>
            <el-table-column label="入库类型" align="center" prop="warehousingType">
                <template slot-scope="scope">
                    <span v-if="scope.row.warehousingType == 0">
                        <el-tag type="success">采购入库</el-tag>
                    </span>
                    <span v-if="scope.row.warehousingType == 1">
                        <el-tag type="danger">成品入库</el-tag>
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="审核状态" align="center" prop="auditStatus">
                <template slot-scope="scope">
                    <span v-if="scope.row.auditStatus == 0">
                        <el-tag type="danger">未审核</el-tag>
                    </span>
                    <span v-if="scope.row.auditStatus == 1">
                        <el-tag type="success">已审核</el-tag>
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="审核人" align="center" prop="auditPerson" />
            <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.auditTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <span v-if="scope.row.auditStatus == 0">
                        <el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleAudit(scope.row)"
                            v-hasPermi="[' purchase:purchase:audit']">质检入库</el-button>
                    </span>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['warehousingRequisition:warehousingRequisition:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['warehousingRequisition:warehousingRequisition:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />


        <!-- 审核入库 -->
        <el-dialog :title="title" :visible.sync="openWare" width="960px" append-to-body>

            <el-form :model="queryTable" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                label-width="68px">
                <el-form-item label="入库表" prop="warehousingRequisitionId">
                    <el-input v-model="queryTable.warehousingRequisitionId"  clearable
                        @keyup.enter.native="handleQuery" readonly />
                </el-form-item>
                <el-form-item label="业务员" prop="warehousingRequisitionPerson">
                    <span style="padding-right:55px;font-size:15px;">{{ Reqion.warehousingRequisitionPerson }}</span>
                </el-form-item>
               
                <el-form-item label="入库单号" prop="warehousingRequisitionOrder">
                    <span style="padding-right:55px;font-size:15px;">{{ Reqion.warehousingRequisitionOrder }}</span>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="loading" :data="tableList" @selection-change="handleChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="入库单列表" align="center" prop="warehousingRequisitionTableId" />
                <!-- <el-table-column label="入库表id" align="center" prop="warehousingRequisitionId" /> -->
                <el-table-column label="存货编码" align="center" prop="warehousingOrderNo" />
                <el-table-column label="存货名称" align="center" prop="warehousingRequisitionTableName" />
                <el-table-column label="规格型号" align="center" prop="warehousingRequisitionTableSpeion" />
                <el-table-column label="主计量单位" align="center" prop="warehousingRequisitionTableUnit" />
                <el-table-column label="数量" align="center" prop="warehousingRequisitionTableNum" />
                <el-table-column label="单价" align="center" prop="warehousingRequisitionTablePrice" />
                <el-table-column label="金额" align="center" prop="warehousingRequisitionTotalPrice" />
                <el-table-column label="税额" align="center" prop="warehousingRequisitionTaxPrice" />
                <el-table-column label="含税总金额" align="center" prop="warehousingRequisitionUnitTotalPrice" />

            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="Addhandle" :disabled="multiple"
                    v-hasPermi="['warehousingRequisition:warehousingRequisition:audit']">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 添加或修改采购入库单对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="仓库" prop="warehouseDeptId">
                    <el-input v-model="form.warehouseDeptId" placeholder="请输入仓库" />
                </el-form-item>
                <el-form-item label="入库单号" prop="warehousingRequisitionOrder">
                    <el-input v-model="form.warehousingRequisitionOrder" placeholder="请输入入库单号" />
                </el-form-item>
                <el-form-item label="业务员" prop="warehousingRequisitionPerson">
                    <el-input v-model="form.warehousingRequisitionPerson" placeholder="请输入业务员" />
                </el-form-item>
                <el-form-item label="供应商" prop="supplierId">
                    <el-input v-model="form.supplierId" placeholder="请输入供应商" />
                </el-form-item>
                <el-form-item label="审核人" prop="auditPerson">
                    <el-input v-model="form.auditPerson" placeholder="请输入审核人" />
                </el-form-item>
                <el-form-item label="审核时间" prop="auditTime">
                    <el-date-picker clearable v-model="form.auditTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择审核时间">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listWarehousingRequisition,auditWare, getWarehousingRequisition, delWarehousingRequisition, addWarehousingRequisition, updateWarehousingRequisition } from "@/api/warehousingRequisition/warehousingRequisition";
import { listWarehousingtable } from "@/api/warehousingRequisition/Warehousingtb";

export default {
    name: "WarehousingRequisition",
    data() {
        return {
            tableList:[],
            Reqion:{},
            openWare:false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 采购入库单表格数据
            warehousingRequisitionList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                warehouseDeptId: null,
                warehousingRequisitionOrder: null,
                warehousingRequisitionPerson: null,
                supplierId: null,
                requisitionType: null,
                warehousingType: null,
                auditStatus: null,
                auditPerson: null,
                auditTime: null,
            },
            queryTable:{},
            // 表单参数
            form: {},
            rows:[],
            pruchaseData:{},
            orderList:{},
            // 表单校验
            rules: {
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        Addhandle(){
      this.pruchaseData.warehousingRequisitionId=this.Reqion.warehousingRequisitionId;
      this.pruchaseData.warehousingRequisitionPerson=this.Reqion.warehousingRequisitionPerson;
      this.pruchaseData.requisitionType=this.Reqion.requisitionType;
      this.pruchaseData.warehousingRequisitionOrder=this.Reqion.warehousingRequisitionOrder;
      this.pruchaseData.supplierId=this.Reqion.supplierId;  
      this.pruchaseData.orderList=this.rows
      console.log(this.pruchaseData);
      auditWare(this.pruchaseData).then(response => {
        this.$modal.msgSuccess("审核操作成功");
        this.openWare = false;
        this.getList();
    })
      
    },
           // 多选框选中数据
      handleChange(selection) {
      this.rows = selection.map(item => item)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
        WareList(warehousingRequisitionId){
            getWarehousingRequisition(warehousingRequisitionId).then(response=>{
                this.Reqion=response.data
            })
    },
          //根据查询采购订单列表
    warehouse(){
        listWarehousingtable(this.queryTable).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
        //审核窗口
        handleAudit(row) {
            this.pruchaseData.warehouseDeptId=row.warehouseDeptId;
            this.queryTable.warehousingRequisitionId= row.warehousingRequisitionId;
      this.warehouse();//根据id查询采购订单列表
      const warehousingRequisitionId=this.queryTable.warehousingRequisitionId
      this.WareList(warehousingRequisitionId)
      this.openWare = true;
      this.title = '采购入库窗口';
        },
        /** 查询采购入库单列表 */
        getList() {
            this.loading = true;
            listWarehousingRequisition(this.queryParams).then(response => {
                this.warehousingRequisitionList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                warehousingRequisitionId: null,
                warehouseDeptId: null,
                warehousingRequisitionOrder: null,
                warehousingRequisitionPerson: null,
                supplierId: null,
                requisitionType: null,
                warehousingType: null,
                auditStatus: null,
                auditPerson: null,
                auditTime: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.warehousingRequisitionId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加采购入库单";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const warehousingRequisitionId = row.warehousingRequisitionId || this.ids
            getWarehousingRequisition(warehousingRequisitionId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改采购入库单";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.warehousingRequisitionId != null) {
                        updateWarehousingRequisition(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addWarehousingRequisition(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const warehousingRequisitionIds = row.warehousingRequisitionId || this.ids;
            this.$modal.confirm('是否确认删除采购入库单编号为"' + warehousingRequisitionIds + '"的数据项？').then(function () {
                return delWarehousingRequisition(warehousingRequisitionIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('warehousingRequisition/warehousingRequisition/export', {
                ...this.queryParams
            }, `warehousingRequisition_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
  